local hx__yixian = fk.CreateSkill{
  name = "hx__yixian",
  tags = { Skill.Compulsory},
  dynamic_desc = function(self, player)
    if player:getMark("hx__yixian-change") > 0 then
      return "hx__yixian_change"
    else
      return "hx__yixian"
    end
  end,
}

Fk:loadTranslationTable{
  [":hx__yixian_change"] = "锁定技，你失去的牌置入弃牌堆时改为置入牌堆顶的第Y张牌上，然后你获得一个“贤”；你失去体力后移去一个“贤”（Y为你的体力值）。",
}

hx__yixian:addEffect(fk.CardUseFinished,{
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__yixian.name) and player.room:getCardArea(data.card) == Card.Processing
  end,
  on_use = function(self, event, target, player, data)
    player.room:moveCards({
      ids = {data.card.id},
      from = player,
      fromArea = Card.PlayerHand,
      toArea = Card.DrawPile,
      moveReason = fk.ReasonJustMove,
      proposer = player.id,
      skillName = hx__yixian.name,
      drawPilePosition = math.max(1, player.hp-1),
    })
    player.room:addPlayerMark(player,"@yanjun__xian", 1)
  end,
})

hx__yixian:addEffect(fk.CardRespondFinished,{
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__yixian.name) and player.room:getCardArea(data.card) == Card.Processing
  end,
  on_use = function(self, event, target, player, data)
    player.room:moveCards({
      ids = {data.card.id},
      from = player,
      fromArea = Card.PlayerHand,
      toArea = Card.DrawPile,
      moveReason = fk.ReasonJustMove,
      proposer = player,
      skillName = hx__yixian.name,
      drawPilePosition = math.max(1, player.hp-1),
    })
    player.room:addPlayerMark(player,"@yanjun__xian", 1)
  end,
})

hx__yixian:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__yixian.name) and player:getMark("hx__yixian-change") > 0 then
      local room = player.room
      local cards = {}
      for _, move in ipairs(data) do
        if move.from == player and move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              if room:getCardArea(info.cardId) == Card.DiscardPile then
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end
      end
      cards = room.logic:moveCardsHoldingAreaCheck(cards)
      if #cards > 0 then
        event:setCostData(self, {cards = cards})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local cards = table.simpleClone(event:getCostData(self).cards)
    if #cards > 0 then
      player.room:moveCards({
        ids = cards,
        from = player,
        fromArea = Card.PlayerHand,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonJustMove,
        proposer = player,
        skillName = hx__yixian.name,
        drawPilePosition = math.max(1, player.hp-1),
      })
      player.room:addPlayerMark(player,"@yanjun__xian", 1)
    end
  end,
})

hx__yixian:addEffect(fk.HpLost, {
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__yixian.name)
  end,
  on_use = function(self, event, target, player, data)
    player.room:removePlayerMark(player,"@yanjun__xian", 1)
  end,
})



return hx__yixian

